#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys 
reload(sys) 
sys.setdefaultencoding('utf8') 

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkecs.request.v20140526 import DescribeInstancesRequest
from aliyunsdkecs.request.v20140526 import StopInstanceRequest
import json

beijing = "cn-beijing"
japan = "ap-northeast-1"
hk = "cn-hongkong"


def query(region):
    # 创建AcsClient实例
    client = AcsClient(
            "LTAIjVNUVcs0a4aU", 
            "ZjgnRPYKzr2fAHOOrroRdG4IZex7al",
            region
            );
    # 创建request，并设置参数
    request = DescribeInstancesRequest.DescribeInstancesRequest()
    request.set_PageSize(50)
    # 发起API请求并显示返回值
    response = client.do_action_with_exception(request)
    # json转化为数组处理
    jsons = json.loads(response)
    for instance in jsons['Instances']['Instance']:
        ExpiredTime = instance['ExpiredTime']
        ZoneId = instance['ZoneId']
        StartTime = instance['StartTime']
        InstanceName = instance['InstanceName']
        OSType = instance['OSType']
        OSName = instance['OSName']
    
        for interface in instance['NetworkInterfaces']['NetworkInterface']:
            PrimaryIpAddress = interface['PrimaryIpAddress']
    
        for ipaddress in instance['PublicIpAddress']['IpAddress']:
            IpAddress = ipaddress

        print("|%s|%s|%s|%s|%s|-|-|"
                %(InstanceName,ZoneId,IpAddress,PrimaryIpAddress,OSName))
    
    

#query(beijing)
#query(japan)
#query(hk)

def ansible(region):
    # 创建AcsClient实例
    client = AcsClient(
            "LTAIjVNUVcs0a4aU", 
            "ZjgnRPYKzr2fAHOOrroRdG4IZex7al",
            region
            );
    # 创建request，并设置参数
    request = DescribeInstancesRequest.DescribeInstancesRequest()
    request.set_PageSize(50)
    # 发起API请求并显示返回值
    response = client.do_action_with_exception(request)
    # json转化为数组处理
    jsons = json.loads(response)
    for instance in jsons['Instances']['Instance']:
        InstanceName = instance['InstanceName']
    
        for interface in instance['NetworkInterfaces']['NetworkInterface']:
            PrimaryIpAddress = interface['PrimaryIpAddress']

        for ipaddress in instance['PublicIpAddress']['IpAddress']:
                    IpAddress = ipaddress
        if InstanceName == "gpu_for_ocr":
            print("[ubuntu-all]")
            print(PrimaryIpAddress)
        elif  region == "cn-hongkong":
            print(IpAddress)
        else:
            print(PrimaryIpAddress)
    

#print("[centos-all]")
#ansible(beijing)
#ansible(japan)
#ansible(hk)

def ansible_list(region):
    # 创建AcsClient实例
    client = AcsClient(
            "LTAIjVNUVcs0a4aU", 
            "ZjgnRPYKzr2fAHOOrroRdG4IZex7al",
            region
            );
    # 创建request，并设置参数
    request = DescribeInstancesRequest.DescribeInstancesRequest()
    request.set_PageSize(50)
    # 发起API请求并显示返回值
    response = client.do_action_with_exception(request)
    # json转化为数组处理
    jsons = json.loads(response)
    for instance in jsons['Instances']['Instance']:
        InstanceName = instance['InstanceName']
    
        for interface in instance['NetworkInterfaces']['NetworkInterface']:
            PrimaryIpAddress = interface['PrimaryIpAddress']

        for ipaddress in instance['PublicIpAddress']['IpAddress']:
                    IpAddress = ipaddress
        if  region == "cn-hongkong":
            print("[%s]"%(InstanceName))
            print(IpAddress)
        else:
            print("[%s]"%(InstanceName))
            print(PrimaryIpAddress)
    

#ansible_list(beijing)
#ansible_list(japan)
#ansible_list(hk)
